//
//  MPCircleView.m
//  tenMS
//
//  Created by plum on 17/6/29.
//  Copyright © 2017年 plum. All rights reserved.
//

#import "MPCircleView.h"
#import "UIView+Frame.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width

@interface MPCircleView ()
@property (nonatomic, assign)CGFloat progress;
@end

@implementation MPCircleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.startAngle = - M_PI;
        self.endAngle = M_PI;
        self.borderWidth = 10;
        self.backgroundStartColor = [UIColor lightGrayColor];
        self.progressStartColor = [UIColor redColor];
        self.progressEndColor = [UIColor greenColor];
        self.lineDashPattern = @[@2,@6];
        [self initView];
    }
    return self;
}
#pragma mark - Private
- (void)initView
{
    CGFloat selfW = MIN(self.width, self.height);
    
    ///虚线圆View=============
    self.layer.cornerRadius = selfW/2;
    self.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    self.transform = CGAffineTransformMakeRotation(-M_PI_2);
    
    CGFloat lineWidth = self.borderWidth;
    //  第一层浅白色的虚线圆
    CAShapeLayer *ovalLayer = [CAShapeLayer layer];
    ovalLayer.strokeColor = [UIColor colorWithRed:0.64 green:0.71 blue:0.87 alpha:0.2].CGColor;
    ovalLayer.fillColor = [UIColor clearColor].CGColor;
    ovalLayer.lineWidth = lineWidth;
    //线的宽度  每条线的间距
    ovalLayer.lineDashPattern  = self.lineDashPattern;
    
    ovalLayer.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(selfW * 0.5, selfW * 0.5) radius:(selfW - self.borderWidth)*0.5 startAngle:self.startAngle endAngle:self.endAngle clockwise:self.clockwise].CGPath;
    
    [self.layer addSublayer:ovalLayer];
    // 第二层黄色的虚线圆 电量多少的
    self.ovalShapeLayer = [CAShapeLayer layer];
    self.ovalShapeLayer.strokeColor = [UIColor yellowColor].CGColor;
    self.ovalShapeLayer.fillColor = [UIColor clearColor].CGColor;
    self.ovalShapeLayer.lineWidth = lineWidth;
    self.ovalShapeLayer.lineDashPattern = self.lineDashPattern;
    
    self.ovalShapeLayer.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(selfW * 0.5, selfW * 0.5) radius:(selfW - self.borderWidth)*0.5 startAngle:self.startAngle endAngle:self.endAngle clockwise:self.clockwise].CGPath;

    self.ovalShapeLayer.strokeEnd = 0;
    [self.layer addSublayer:self.ovalShapeLayer];
    
    //设置渐变颜色
    UIColor *endcolor =   [UIColor redColor];
    UIColor *startcolor = [UIColor greenColor];

    CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    [gradientLayer setColors:[NSArray arrayWithObjects:(id)[startcolor CGColor],(id)[endcolor CGColor], nil]];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1);
    // 用progressLayer来截取渐变层
    [gradientLayer setMask:self.ovalShapeLayer];
    [self.layer addSublayer:gradientLayer];
}

- (void)updateProgress:(CGFloat)startProgress endProgress:(CGFloat)endProgress showAnimation:(BOOL)showAnimation {
    self.progress = endProgress;
    if (showAnimation) {
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = 1;
        pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        pathAnimation.fromValue = @(startProgress);
        pathAnimation.toValue = @(endProgress);
        pathAnimation.fillMode = kCAFillModeForwards;
        pathAnimation.removedOnCompletion = NO;
        pathAnimation.autoreverses = NO;
        [self.ovalShapeLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
    }else {
        self.ovalShapeLayer.strokeEnd = endProgress;
    }
}

@end
